package com.behavioranalysis.flinkprogram.dao.impl;

import com.behavioranalysis.flinkprogram.dao.ITimingTaskDAO;
import com.behavioranalysis.flinkprogram.domain.TimingTask;
import com.behavioranalysis.flinkprogram.jdbc.JDBCHelper;

import java.sql.ResultSet;

public class TimingTaskDAOImpl implements ITimingTaskDAO {

    /**
     * 插入定时任务
     * @param timingTask
     */
    @Override
    public void insert(TimingTask timingTask) {
        String sql = "insert into timing_task (task_id,task_param) "
                + "values(?,?)";

        Object[] params = new Object[]{timingTask.getTaskid(),
                timingTask.getTaskParam()};

        JDBCHelper jdbcHelper = JDBCHelper.getInstance();
        jdbcHelper.executeUpdate(sql, params);
    }

    /**
     * 根据主键查询定时任务
     * @param taskid 主键
     * @return
     */
    @Override
    public TimingTask findById(long taskid) {
        final TimingTask timingTask = new TimingTask();

        String sql = "select * from timing_task where task_id=?";
        Object[] params = new Object[]{taskid};

        JDBCHelper jdbcHelper = JDBCHelper.getInstance();
        jdbcHelper.executeQuery(sql, params,
                new JDBCHelper.QueryCallback() {
                    @Override
                    public void process(ResultSet rs) throws Exception {
                        if (rs.next()) {
                            long taskid = rs.getLong(1);
                            String taskName = rs.getString(2);
                            String createTime = rs.getString(3);
                            String startTime = rs.getString(4);
                            String finishTime = rs.getString(5);
                            String taskType = rs.getString(6);
                            String taskStatus = rs.getString(7);
                            String taskParam = rs.getString(8);

                            timingTask.setTaskid(taskid);
                            timingTask.setTaskName(taskName);
                            timingTask.setCreateTime(createTime);
                            timingTask.setStartTime(startTime);
                            timingTask.setFinishTime(finishTime);
                            timingTask.setTaskType(taskType);
                            timingTask.setTaskStatus(taskStatus);
                            timingTask.setTaskParam(taskParam);
                        }
                    }
                });
        return timingTask;
    }
}
